package com.client.clovece;

public class AI {

	LocalGame parent;
	
	public AI(LocalGame parent){
		this.parent = parent;
	}
	
	protected boolean sbdyHome(Player activePlayer){
		for(int i = 0; i< parent.gp.getStartTiles(activePlayer.getId()).size(); i++){
			if(parent.gp.getStartTiles(activePlayer.getId()).get(i).isTempStateSet()){
				return true;
			}
		}
		return false;
	}
	
	protected TileButton getSbdyHome(Player activePlayer){
		for(int i = 0; i< parent.gp.getStartTiles(activePlayer.getId()).size(); i++){
			if(parent.gp.getStartTiles(activePlayer.getId()).get(i).isTempStateSet()){
				return parent.gp.getStartTiles(activePlayer.getId()).get(i);
			}
		}
		return null;
	}
	
	protected boolean isOccupied(int gridIndex){
		if(parent.gp.game.get(gridIndex).isTempStateSet()) return true;
		return false;
	}
	
	//This method searches through the player's pieces and looks
	//if there is someone to kick.
	//t is currently examined piece
	//candidate is a candidate field to kick
	protected boolean canKick(Player pl, int dice){
		for(int i = 0; i<4; i++){
			TileButton t = parent.gp.grid.get(pl.getFigure(i).getPos());
			int gameGridIndex = parent.gp.game.indexOf(t);
			TileButton candidate = parent.gp.game.get((gameGridIndex+dice)%parent.gp.game.size());
			if(gameGridIndex != -1){
				if(candidate.isTempStateSet() && ButtonStates.getOwner(candidate.getTempState()) != pl.getId()){
					return true;
				}
			}
		}
		return false;
	}
	
	protected boolean canKickFromHome(Player pl){
		TileButton t = parent.gp.getCrossroad(pl.getId());
		if(t.isTempStateSet() && ButtonStates.getOwner(t.getTempState()) != pl.getId()) return true;
		else return false;
	}
	
	protected boolean myCrossroadBlocked(Player pl){
		TileButton t = parent.gp.getCrossroad(pl.getId());
		if(t.isTempStateSet() && ButtonStates.getOwner(t.getTempState()) == pl.getId()) return true;
		else return false;
	}
	
	protected boolean canLeaveCrossroad(Player pl, int dice){
		TileButton t = parent.gp.getCrossroad(pl.getId());
		int i = parent.gp.game.indexOf(t);
		TileButton u;
		if(ButtonStates.getOwner(t.getTempState()) == pl.getId()){
			u = parent.gp.game.get((i+dice)%parent.gp.game.size());
			if(!u.isTempStateSet()) return true;
		}
		return false;
	}
	//!TODO repair -- magic variable
	protected TileButton getCanKick(Player pl, int dice){
		for(int i = 0; i<4; i++){
			TileButton t = parent.gp.grid.get(pl.getFigure(i).getPos());
			int gameGridIndex = parent.gp.game.indexOf(t);
			TileButton candidate = parent.gp.game.get((gameGridIndex+dice)%parent.gp.game.size());
			if(gameGridIndex != -1){
				if(candidate.isTempStateSet() && ButtonStates.getOwner(candidate.getTempState()) != pl.getId()){
					return parent.gp.game.get(gameGridIndex);
				}
			}
		}
		return null;
	}
	
	protected boolean canGoEnd(Player pl, int dice){
		for(int i = 0; i<4; i++){
			TileButton t = parent.gp.grid.get(pl.getFigure(i).getPos());
			int gameGridIndex = parent.gp.game.indexOf(t);
			if(gameGridIndex != -1){
				if(pl.getFigure(i).getDist() + dice >= parent.gp.game.size()){
					return true;
				}
			}
		}
		return false;
	}
	
	protected TileButton getCanGoEnd(Player pl, int dice){
		for(int i = 0; i<4; i++){
			TileButton t = parent.gp.grid.get(pl.getFigure(i).getPos());
			int gameGridIndex = parent.gp.game.indexOf(t);
			if(gameGridIndex != -1){
				if(pl.getFigure(i).getDist() + dice >= parent.gp.game.size()){
					return parent.gp.game.get(gameGridIndex);
				}
			}
		}
		return null;
	}
	//!TODO remove println 
	/** 
	 * This method searches through pieces of a Player pl and returns one that 
	is closest to the end.*/
	protected TileButton getClosestToEnd(Player pl, int dice){
		int dist = -1;
		int pos = -1;
		System.out.println("Test");
		for(int i = 0; i<4; i++){
			if(pl.getFigure(i).getDist() > dist){
				System.out.println("Dist");
				TileButton t = parent.gp.grid.get(pl.getFigure(i).getPos());
				TileButton u = parent.gp.game.get((t.getRoleArrId()+dice)%parent.gp.game.size());
				//is the button u free?
				if(ButtonStates.getOwner(t.getTempState()) != ButtonStates.getOwner(u.getTempState())){
					System.out.println("Occupant");
					dist = pl.getFigure(i).getDist();
					pos = pl.getFigure(i).getPos();
				}
			}			
		}
		if(pos == -1) return null;
		else return parent.gp.grid.get(pos);
	}
	
	public void printStats(Player activePlayer, int dice){
		System.out.println("Hraje hrac "+ activePlayer.getId()+ ". Na kostce: "+dice);
		for(int i = 0; i < 4; i++){
			System.out.println("Figurka "+i+" ma ujito: "+activePlayer.getFigure(i).getDist()+" pozice: "+activePlayer.getFigure(i).getPos());
		}
	}
	
	/*
	 * Logika hry AI
	 * */
	public void move(Player activePlayer, int dice){
		TileButton t = null;
		TileButton u = null;
		if(parent.allHome() && dice != 6){
			//
		}else if(sbdyHome(activePlayer) && dice == 6 && canKickFromHome(activePlayer)){
			//kdyz nam nekdo stoji na vystupu z domecku a muzeme nasadit novou figurku
			t = getSbdyHome(activePlayer);
			u = parent.gp.getCrossroad(activePlayer.getId());
			parent.kick(u.getGridArrId());
			u.setTempStateAndIcon(ButtonStates.getOwner(t.getTempState()));
			t.setIconAndStateDefault();
			//v domecku je nastavena dist na -1, na krizovatce by mela mit hodnotu 1 lze zmenit i pomoci settokenpos()
			activePlayer.setTokenDist(t.getGridArrId(), 0);
			activePlayer.setTokenPos(t.getGridArrId(), u.getGridArrId());			
		}else if(sbdyHome(activePlayer) && dice == 6 && !myCrossroadBlocked(activePlayer)){
			//kdyz muzeme nasadit novou figurku
			t = getSbdyHome(activePlayer);
			u = parent.gp.getCrossroad(activePlayer.getId());
			u.setTempStateAndIcon(ButtonStates.getOwner(t.getTempState()));
			t.setIconAndStateDefault();
			activePlayer.setTokenPos(t.getGridArrId(), u.getGridArrId(),1);
			t.update(t.getGraphics());
			u.update(u.getGraphics());
		}else if(canGoEnd(activePlayer, dice)){
			//kdyz muzeme domu 
			t = getCanGoEnd(activePlayer, dice);
			int i = parent.gp.getCrossroad(activePlayer.getId()).getRoleArrId();
			if(i<t.getRoleArrId()){
				i += parent.gp.game.size();
			}
			int j = dice-(i-t.getRoleArrId());
			System.out.println("Muzeme domu: Figurka AI muze jit do chlivecku " + j);
			if(j<parent.gp.getEndTiles(activePlayer.getId()).size()){
			u = parent.gp.getEndTiles(activePlayer.getId()).get(j);
			
			u.setTempStateAndIcon(ButtonStates.getOwner(t.getTempState()));
			t.setIconAndStateDefault();
				activePlayer.setTokenPos(t.getGridArrId(), u.getGridArrId());
			}
		}else if(canKick(activePlayer, dice)){
			//kdyz muzeme nekoho vykopnout
			t = getCanKick(activePlayer, dice);
			u = parent.gp.game.get(t.getRoleArrId()+dice);
			
			parent.kick(u.getGridArrId());
			u.setTempStateAndIcon(ButtonStates.getOwner(t.getTempState()));
			t.setIconAndStateDefault();
			activePlayer.setTokenPos(t.getGridArrId(), u.getGridArrId(), dice);	
		}else if(myCrossroadBlocked(activePlayer)){
			//kdyz si stojim na vychozim policku
			t = parent.gp.getCrossroad(activePlayer.getId());
			u = parent.gp.game.get((t.getRoleArrId()+dice)%parent.gp.game.size());
			
			u.setTempStateAndIcon(ButtonStates.getOwner(t.getTempState()));
			t.setIconAndStateDefault();
			activePlayer.setTokenPos(t.getGridArrId(), u.getGridArrId(), dice);
		}else if(!parent.allHome()){
			t = getClosestToEnd(activePlayer, dice);
			u = parent.gp.game.get((t.getRoleArrId()+dice)%parent.gp.game.size());
			
			u.setTempStateAndIcon(ButtonStates.getOwner(t.getTempState()));
			t.setIconAndStateDefault();
			activePlayer.setTokenPos(t.getGridArrId(), u.getGridArrId(), dice);
			t.update(t.getGraphics());
			u.update(u.getGraphics());
		}
		if(t != null) t.update(t.getGraphics());
		if(u != null) u.update(u.getGraphics());
		printStats(activePlayer, dice);
		try {
				Thread.sleep(500);
		} catch (InterruptedException e) {
				System.err.println("Chyba uspani.");
				e.printStackTrace();
		}
	}
}
